<template>
  <div class="app-container">
  <!--  操作 模糊搜素  -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品延期" prop="instrument">
        <el-input
            v-model="queryParams.instrument"
            placeholder="请输入商品"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" >
          <el-date-picker
              v-model="queryParams.begDate"
              type="date"
              :disabled-date="disabledDateBeg"
              size="small"
              value-format="YYYY-MM-DD"
              placeholder="选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" >
          <el-date-picker
              v-model="queryParams.endDate"
              type="date"
              :disabled-date="disabledDateEnd"
              size="small"
              value-format="YYYY-MM-DD"
              placeholder="选择结束日期">
          </el-date-picker>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="Edit" size="small" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-form-item>
    </el-form>

  <!--  按钮  -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            size="small"
            @click="handleExport"
            v-hasPermi="['marketdata:commodity:delPrice:exportPrice']"
        >当页导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            size="small"
            @click="handleExportAll"
            v-hasPermi="['marketdata:commodity:delPrice:exportPrice']"
        >全量导出
        </el-button>
      </el-col>
    </el-row>


      <xTable ref="cmdtDelRef" v-loading="loading" :data="delayList" multiple="false"
            refId="cmdtDelRef" @selection-change="handleSelectionChange">
            <template #columList>
      <el-table-column label="序号" type="index" width="40"></el-table-column>
      <el-table-column label="日期" align="center" prop="begDate" :show-overflow-tooltip="true" />
      <el-table-column label="合约代码" width="80" show-overflow-tooltip>
          <template #default="scope">
            <el-link type="primary" @click="queryTrade(scope.row.iCode,scope.row.aType,scope.row.mType)">{{scope.row.iCode}}</el-link>
          </template>
      </el-table-column>
      <el-table-column label="合约名称" align="center" prop="name" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="pType">
      </el-table-column>
      <el-table-column label="市场类型" align="center" prop="mType" :show-overflow-tooltip="true" >
        <template #default="scope">
          <dict-tag :options="M_TYPE" :value="scope.row.mType" />
        </template>
      </el-table-column>
      <el-table-column label="币种" align="center" :show-overflow-tooltip="true" prop="currency">
        <template #default="scope">
            <dict-tag :options="CURRENCY" :value="scope.row.currency" />
        </template>
      </el-table-column>
      <el-table-column label="开盘价" align="right" :show-overflow-tooltip="true" prop="dpOpen" :formatter=" formartTableP2"></el-table-column>
      <el-table-column label="收盘价" align="right" :show-overflow-tooltip="true" prop="dpClose" :formatter=" formartTableP2"></el-table-column>
      <el-table-column label="最高价" align="right" :show-overflow-tooltip="true" prop="dpHigh" :formatter=" formartTableP2"></el-table-column>
      <el-table-column label="最低价" align="right" :show-overflow-tooltip="true" prop="dpLow" :formatter=" formartTableP2"></el-table-column>
      <el-table-column label="平均价" align="right" :show-overflow-tooltip="true" prop="dpAvg" :formatter=" formartTableP2"></el-table-column>
      <el-table-column label="结算价" align="right" :show-overflow-tooltip="true" prop="dpSet" :formatter=" formartTableP2"></el-table-column>
            </template>
    </xTable>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!--修改对话框 -->
    <el-dialog :title="delaytitle" v-model="UpdateOpen" :close-on-click-modal="false" draggable destroy-on-close width="500px" append-to-body>
      <el-form ref="cmdtFromRef" :model="delayFrom" :rules="cmdtdelRules" label-width="110px">
        <el-form-item label="日期" prop="begDate">
          <el-input disabled v-model="delayFrom.begDate"/>
        </el-form-item>
        <el-form-item label="合约代码" prop="iCode">
          <el-input disabled v-model="delayFrom.iCode"/>
        </el-form-item>
        <el-form-item label="合约名称" prop="siName">
          <el-input disabled v-model="delayFrom.name"/>
        </el-form-item>
        <el-form-item label="市场类型" prop="mType">
          <el-input disabled v-model="delayFrom.mType"/>
        </el-form-item>
        <el-form-item label="开盘价" prop="dpOpen" >
          <number v-model="delayFrom.dpOpen" precision="4"></number>
        </el-form-item>
        <el-form-item label="收盘价" prop="dpClose" >
          <number v-model="delayFrom.dpClose" precision="4"></number>
        </el-form-item>
        <el-form-item label="最高价" prop="dpHigh" >
          <number v-model="delayFrom.dpHigh" precision="4"></number>
        </el-form-item>
        <el-form-item label="最低价" prop="dpLow">
          <number v-model="delayFrom.dpLow" precision="4"></number>
        </el-form-item>
        <el-form-item label="平均价" prop="dpAvg">
          <number v-model="delayFrom.dpAvg" precision="4"></number>
        </el-form-item>
        <el-form-item label="结算价" prop="dpSet" >
          <number v-model="delayFrom.dpSet" precision="4"></number>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submit">确 定</el-button>
          <el-button @click="cancelUpdate">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DelayPrice">
import {getCmdtdelPriceList,updateTcmdtDelPrice} from '@/api/marketdata/commodity/commodity'
import {getLastWorkDay} from '@/api/marketdata/equity/fund';
import xTable from "@/xquant/base/xTable";
const router = useRouter();
const {proxy} = getCurrentInstance();
const {M_TYPE} = proxy.useDict("M_TYPE");
const {A_TYPE} = proxy.useDict("A_TYPE");
const {CURRENCY} = proxy.useDict("CURRENCY");
import number from '@/xquant/base/number';
const delayList = ref([]);
const UpdateOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const rows = ref([]);
const indexs = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const delaytitle = ref("");
const instrument = ref("");
const data = reactive({
  cmdtdelRules: {
    dpOpen: [{required: false, message: "开盘价不能为空", trigger: "blur"}],
    dpClose: [{required: true, message: "收盘价不能为空", trigger: "blur"}],
    dpHigh: [{required: false, message: "最高价不能为空", trigger: "blur"}],
    dpLow: [{required: false, message: "最低价不能为空", trigger: "blur"}],
    dpAvg: [{required: false, message: "平均价不能为空", trigger: "blur"}],
    dpSet: [{required: false, message: "结算价不能为空", trigger: "blur"}]
  },
  delayFrom: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    instrument: undefined,
    begDate: proxy.$moment(new Date()).format("YYYY-MM-DD"),
    endDate: proxy.$moment(new Date()).format("YYYY-MM-DD"),
  }
});

onMounted(() => {
  getLastWorkDay().then(response => {
    queryParams.value.begDate = response.msg;
    getList();
  });
})

const disabledDateBeg = (time) => {
  if(queryParams.value.endDate){
    return time.getTime() > new Date(queryParams.value.endDate).getTime()
  }
}

const disabledDateEnd = (time) => {
  if(queryParams.value.begDate){
    return time.getTime() < new Date(queryParams.value.begDate).getTime()-86400000
  }
}

const {queryParams,selectDate,delayFrom} = toRefs(data);

/** 导出按钮操作 */
function handleExport() {
  proxy.download("marketdata/tcmdtController/exportdelPrice", {
    ...queryParams.value
  }, `商品延期行情${new Date().getTime()}.xlsx`);
}

/** 导出按钮操作 */
function handleExportAll() {
  proxy.download("marketdata/tcmdtController/exportdelPrice", Object.assign({
    ...queryParams.value
  }, {pageNum:'',pageSize: ''}), `商品延期行情${new Date().getTime()}.xlsx`);
}

/** 查询列表 */
function getList() {
  loading.value = true;
  getCmdtdelPriceList(queryParams.value).then(response => {
    delayList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.instrument = null;
  queryParams.value.begDate = null;
  queryParams.value.endDate = null;
  getList();
}

// 修改
function handleUpdate() {
  reset();
  UpdateOpen.value = true;
  delayFrom.value = rows.value[0];
  delaytitle.value = "修改商品延期行情";
}

// 重置表单
function reset() {
  delayFrom.value = {};
  proxy.resetForm("cmdtFromRef");
}

function submit() {
  UpdateOpen.value = false;
  updateTcmdtDelPrice(delayFrom.value).then(response => {
    if (response.success) {
      proxy.$modal.msgSuccess("修改成功");

      reset();
      getList();
    } else {
      proxy.$modal.msgError(response.msg);
    }
  });
  reset()
}

// 多选框选中
function handleSelectionChange(selection) {
  if (selection.length == 0) {
    ids.value = [];
    indexs.value = [];
    rows.value = [];
  } else {
    ids.value = selection.map(item => item.iCode);
    indexs.value = selection.map(item => item.index);
    rows.value = selection.map(item => item);
  }
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  if (selection.length == 1) {
    single.value = false;
  }
  if (selection.length != 1) {
    single.value = true;
  }
}

//根据代码查询交易
function queryTrade(iCode,a,m){
  instrument.value = iCode+','+a+','+m
  router.push({
    path: "/TradeManage/tradeBalance",
    query: { iCode: instrument.value},
  });
}

function cancelUpdate() {
  UpdateOpen.value = false;
  reset();
}

</script>
<style scoped>
.el-link {
  margin-right: 8px;
}
.el-link .el-icon--right.el-icon {
  vertical-align: text-bottom;
}
</style>
